﻿using System;
using System.Collections.Generic;
using System.Web.Services;
using BLL;
using BLL.RF.Package;
using Model;
using Model.RF.Package;

/// <summary>
///ProductPackage_CheckandPackage_Service 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。 
// [System.Web.Script.Services.ScriptService]
public class ProductPackage_CheckandPackage_Service : System.Web.Services.WebService
{
    private PackageProduct pp;
    private ProductType pt;
    private ProductType_Flow pf;
    private ProductType_Step ps;
    private Customer c;
    private SNUnion su;
    private Package p;

    public ProductPackage_CheckandPackage_Service()
    {
        pp = new PackageProduct();
        pt = new ProductType();
        pf = new ProductType_Flow();
        ps = new ProductType_Step();
        c = new Customer();
        su = new SNUnion();
        p = new Package();
        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    private IList<SNUnionInfo> GetSNUnionList(string packageProductSN)
    {
        SNUnion su = new SNUnion();
        IList<SNUnionInfo> list = su.SNUnion_GetbyPackageProductSN(packageProductSN);
        PackageProductInfo ppi = pp.GetPackageProductbyPackageProductSN(packageProductSN);
        if (!string.IsNullOrEmpty(ppi.PackageProductSN))
        {
            list.Add(new SNUnionInfo()
            {
                PackageProductSN = ppi.PackageProductSN,
                ProductSN = ppi.ProductSN,
                ProductTypeID = ppi.ProductTypeID,
                RecordTime = ppi.UnionTime,
                UserID = ppi.UserID
            });
        }
        return list;
    }

    [WebMethod(Description = "判断产品是否关联过")]
    public bool HasValid_UnionData(string packageProductSN)
    {
        return GetSNUnionList(packageProductSN).Count > 0 ? true : false;
        //return !string.IsNullOrEmpty(pp.GetPackageProductbyPackageProductSN(packageProductSN).PackageProductSN);  
    }

    [WebMethod(Description = "判断产品是否包装过")]
    public bool HasValid_PackageData(string packageProductSN)
    {
        return (!string.IsNullOrEmpty(pp.GetPackageProductbyPackageProductSN(packageProductSN).ProductSN)
            && pp.GetPackageProductbyPackageProductSN(packageProductSN).IsPackage.Equals("Yes", StringComparison.OrdinalIgnoreCase)
            || !string.IsNullOrEmpty(p.LoadEntity(packageProductSN).PackageProductSN));
    }

    [WebMethod(Description = "获取产品所属客户；依据产品所属客户的不同，给用户呈现的界面不同，对用户输入要求也不同")]
    public string GetCustomerName(string packageProductSN)
    {
        IList<SNUnionInfo> list = GetSNUnionList(packageProductSN);
        if (list.Count > 0)
        {
            string pid = list[0].ProductTypeID;
            string cName = pt.LoadEntity(pid).CustomerName;
            cName = cName.Equals("泰科(Tektelic)") ? "中兴(ZTE)" : cName;
            return cName;
        }
        else
        {
            return null;
        }
    }

    [WebMethod(Description = "判断产品与3BK条码对应是否正确")]
    public bool IsValid_SN3BK(string packageProductSN, string sn3bk)
    {
        string pid = GetSNUnionList(packageProductSN)[0].ProductTypeID;
        return sn3bk.Equals(pt.LoadEntity(pid).SN3BK, StringComparison.OrdinalIgnoreCase);
    }

    [WebMethod(Description = "判断产品2D条码是否格式正确")]
    public bool IsValid_2DSN(string packageProductSN, string sn3bk, string sn2d)
    {
        return sn2d.Contains(packageProductSN) && sn2d.Contains(sn3bk);
    }

    [WebMethod(Description = "判断产品关键工序数据检查是否合格：包括常规工序、有源工序管控，包括九大关键工序检查，包括高频、气密返修相关管控")]
    public bool IsValid_KeyProcess_DataCheck(string packageProductSN,out string message)
    {
        IList<SNUnionInfo> list = GetSNUnionList(packageProductSN);
        if (list.Count==0)
        {
            message = "此成品序列号没有关联过，请检查！";
            return false;
        }
        foreach (SNUnionInfo sui in list)
        {
            string productSN = sui.ProductSN;
            string productTypeID = sui.ProductTypeID;
            if (!pf.ProductType_Flow_HasValidSevenStep(productSN, productTypeID, "PackageStep"))
            {
                message = "不能包装，请反查产品" + productSN + "的常规管控工序的数据!";
                return false;
            }
            if (!pp.Package_HasValidRFSourceStep(productSN, productTypeID))
            {
                message = "不能包装，请反查产品" + productSN + "的有源管控工序的数据！";
                return false;
            }
            if (!pp.IsValid_NineStep_DataCheck(productSN, out message))
            {
                return false;
            }
        }
        message = "";
        return true;
    }

    [WebMethod(Description = "包装记录数据上传")]
    public bool DataUpload(string packageProductSN, string userID)
    {
        string pid = GetSNUnionList(packageProductSN)[0].ProductTypeID;
        switch (pt.LoadEntity(pid).HouseAmount)
        {
            case 2:
                PackageInfo pi = new PackageInfo()
                {
                    PackageProductSN = packageProductSN,
                    RecordTime = DateTime.Now,
                    UserID = userID
                };
                return p.Insert(pi);
            default:
                try
                {
                    DateTime dt = DateTime.Now;
                    pp.SetPackaged(packageProductSN, userID, dt);
                    return true;
                }
                catch
                {
                    return false;
                }
        }
    }
}

